package beans;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.oreilly.servlet.MultipartRequest;



public class UserDAO {
	
	public boolean login(String userno,String pwd) {
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		
		try {
			con=DBConnection.getConnection();
			String sql="select * from user";
			pst=con.prepareStatement(sql);
			rs=pst.executeQuery();
			if(rs.next()) {
				System.out.println("ok");
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;		
	}
	
	public Map<String, Object> findone(String userno) {
		System.out.println("进入findone 参数userno is:"+userno);
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			con=DBConnection.getConnection();
			String sql="select * from user where user_no=?";
			pst=con.prepareStatement(sql);
			pst.setString(1, userno);
			rs=pst.executeQuery();
			String[] keys= {"userno","userpwd","username","usersex","userage","useraddress","userprofession","userinterest","useremail","userphoto","usersummary"};
			
			if(rs.next()) {
				for(int i=0;i<keys.length;i++) {
					map.put(keys[i], rs.getString(i+2));
				}
			}			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		System.out.println("findone is:"+map.toString());
		return map;		
	}

	
	public boolean insert(String userno,String userpwd,String username,String usersex,String userage,String useraddress,String userprofession,String userinterest,String useremail,String userphoto,String usersummary) {
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		try {
			con=DBConnection.getConnection();
			String sql="insert into user values(null,?,?,?,?,?,?,?,?,?,?,?)";
			pst=con.prepareStatement(sql);
			pst.setString(1, userno);
			pst.setString(2, userpwd);
			pst.setString(3, username);
			pst.setString(4, usersex);
			pst.setString(5, userage);
			pst.setString(6, useraddress);
			pst.setString(7, userprofession);
			pst.setString(8, userinterest);
			pst.setString(9, useremail);
			pst.setString(10, userphoto);
			pst.setString(11, usersummary);
			int n=pst.executeUpdate();
			if(n>=1) {
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;
	}
	
	public boolean insert(User user) {
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		try {
			con=DBConnection.getConnection();
			String sql="insert into user values(null,?,?,?,?,?,?,?,?,?,?,?)";
			pst=con.prepareStatement(sql);
			pst.setString(1, user.userno);
			pst.setString(2, user.userpwd);
			pst.setString(3, user.username);
			pst.setString(4, user.usersex);
			pst.setString(5, user.userage);
			pst.setString(6, user.useraddress);
			pst.setString(7, user.userprofession);
			pst.setString(8, user.userinterest);
			pst.setString(9, user.useremail);
			pst.setString(10, user.userphoto);
			pst.setString(11, user.usersummary);
			int n=pst.executeUpdate();
			if(n>=1) {
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;
	}
	
	public boolean insert(HttpServletRequest request) {
		boolean flag=true;
		//1定义保存图片的目录
		String saveDirectory = request.getSession().getServletContext().getRealPath("")+"\\upload";
		File savedir = new File(saveDirectory);//File可表示目录，也可表示文件
		if (!savedir.exists()) { savedir.mkdirs();}

		//2 定义MultipartRequest对象，接各类参数，保存上传的文件
		int maxPostSize = 5 * 1024 * 1024; // 什么意思？5M
		MultipartRequest multiRequest=null;	
		try {
			multiRequest = new MultipartRequest(request,saveDirectory, maxPostSize, "utf-8");
		} catch (IOException e) {			
			e.printStackTrace();
		}

		String userphoto="";//如果没有传照片，则用空来表示
		Enumeration<String> userFileEnumeration = multiRequest.getFileNames();//标记名列表
		while(userFileEnumeration.hasMoreElements()) {//实际只传一个图片文件，改成if也可
			String htmlname = userFileEnumeration.nextElement();//html的name属性，如userphoto
			File sendFile=multiRequest.getFile(htmlname);//是接收到的真正的文件
			userphoto = multiRequest.getFilesystemName(htmlname);//是接收到的真正的文件名，如test.png
		}
			
		//接其他参数
		String userno = multiRequest.getParameter("userno");
		request.getSession().setAttribute("userno", userno);
		String userpwd = multiRequest.getParameter("userpwd");
		String username = multiRequest.getParameter("username");
		String usersex = multiRequest.getParameter("usersex");
		String userage = multiRequest.getParameter("userage");
		String useraddress = multiRequest.getParameter("useraddress");
		String userprofession = multiRequest.getParameter("userprofession");
		String useremail = multiRequest.getParameter("useremail");
		//String userphoto = ...;
		String usersummary = multiRequest.getParameter("usersummary");

		String []userinterests = multiRequest.getParameterValues("userinterest");
		String userinterest="";
		for(int i=0;i<userinterests.length;i++){
			userinterest=userinterest+userinterests[i]+"-";
		}
		userinterest=userinterest.substring(0, userinterest.length()-1);

		//3写入数据库中
		User user=new User(0,userno, userpwd, username, usersex, userage, useraddress, userprofession, userinterest, useremail, userphoto, usersummary);
		flag=insert(user);
		
		System.out.println("userphoto is"+user.userphoto);

		//4注册插入后，带照片，将新数据只在到user属性中，以便于主页中能够及时显示照片等信息。
		request.getSession().setAttribute("user", user);
		return flag;
	}
	
	
	public ArrayList<Map<String,Object>> findall(){
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		ArrayList<Map<String, Object>> al=new ArrayList<>();

		try {
			con=DBConnection.getConnection();
			String sql="select * from user";
			pst=con.prepareStatement(sql);
			
			rs=pst.executeQuery();
			String[] keys= {"userno","userpwd","username","usersex","userage","useraddress","userprofession","userinterest","useremail","userphoto","usersummary"};
			while(rs.next()) {
				Map<String, Object> map=new HashMap<>();
				for(int i=0;i<keys.length;i++) {
					map.put(keys[i], rs.getString(i+2));
				}
				al.add(map);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return al;
	}
	
	public ArrayList<Map<String,Object>> findwhere(String sql){
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		ArrayList<Map<String, Object>> al=new ArrayList<>();

		try {
			con=DBConnection.getConnection();
			
			pst=con.prepareStatement(sql);
			
			rs=pst.executeQuery();
			String[] keys= {"userno","userpwd","username","usersex","userage","useraddress","userprofession","userinterest","useremail","userphoto","usersummary"};
			while(rs.next()) {
				Map<String, Object> map=new HashMap<>();
				for(int i=0;i<keys.length;i++) {
					map.put(keys[i], rs.getString(i+2));
				}
				al.add(map);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return al;
	}
	
	public boolean update(String userno,String userpwd,String username,String usersex,String userage,String useraddress,String userprofession,String userinterest,String useremail,String userphoto,String usersummary) {
		System.out.println("进入更新函数，得到的参数是：");
		System.out.println(userno+userpwd+username+usersex+userage+useraddress+userprofession+userphoto+useremail+usersummary);
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		try {
			con=DBConnection.getConnection();			
			String sql="update user set user_no=?,user_pwd=?,user_name=?,user_sex=?,user_age=?,user_address=?,user_profession=?,user_interest=?,user_email=?,user_photo=?,user_summary=? where user_no=?";
			pst=con.prepareStatement(sql);
			pst.setString(1, userno);
			pst.setString(2, userpwd);
			pst.setString(3, username);
			pst.setString(4, usersex);
			pst.setString(5, userage);
			pst.setString(6, useraddress);
			pst.setString(7, userprofession);
			pst.setString(8, userinterest);
			pst.setString(9, useremail);
			pst.setString(10, userphoto);
			pst.setString(11, usersummary);
			pst.setString(12, userno);
			int n=pst.executeUpdate();
			if(n>=1) {
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;
	}

}
